package com.mayikt.days04;

import java.util.concurrent.locks.LockSupport;

/**
 * @ClassName MyTask
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class MyTask<V> extends Thread implements Runnable {
    private MyCallable<V> myCallable;
    private V v;
    private Thread cuThread;

    public MyTask(MyCallable myCallable) {
        this.myCallable = myCallable;
    }

    @Override
    public void run() {
        v = myCallable.call();
        // 唤醒阻塞的线程
        LockSupport.unpark(cuThread);
    }

    public V get() {
        if (v != null) {
            return v;
        }
        cuThread = Thread.currentThread();
        LockSupport.park(cuThread);
        return v;
    }
}
